<!--迁移服务器-->
<template>
  <div class="all">
    <div class="bg" v-if="isShow">

      <!--（从）服务器-->
      <div class="content all bg2">

        <div class="title">

          <div class="row acenter" @click="privateClick(true)">
            <text class="font28 main-t1">{{T('从')}}</text>
            <div class="width30"></div>
            <text class="font30 text1">{{toPrivate ? T('公共服务器') : server.name}}</text>
            <text class="font30 text1" v-if="!toPrivate">{{'-(' + totalPairCount + ')'}}</text>
            <LImage class="arrow-image" v-if="!toPrivate" :src="Config.img('xiala.png')"></LImage>
          </div>

          <div class="all"></div>
          <div class="row acenter" @click="allClick" v-if="strategys.length">
            <LImage class="select-image" :src="Config.img(strategys.length == selectNum ? 'xuanzhong.png' : 'weixuan.png')"></LImage>
            <text class="font30 text1">{{' ' + T('全选')}}</text>
          </div>
        </div>

        <div class="line bg3"></div>
        <list class="list" v-if="strategys.length">
          <cell class="cell" v-for="(item, index) in strategys" v-bind:key="index" @click="cellClick(item)">
            <div class="column">
              <div class="row acenter">
                <text class="font30 text1">{{item.strategyName + '-' + item.apiName}}</text>
                <text class="font30 text1">{{'-(' + item.childCount + ')'}}</text>
              </div>
              <text class="font24 text2">{{getTime(item.startTime) + ' -'}}</text>
            </div>
            <LImage class="select-image" :src="Config.img(item.select ? 'xuanzhong.png' : 'weixuan.png')"></LImage>
          </cell>
        </list>
        <div class="list-tipe" v-else>
          <text class="font30 text1">{{'暂无策略实例'}}</text>
        </div>
      </div>

      <!--交换按钮-->
      <div class="acenter">
        <LImage class="swap" ref="swap" :src="Config.img('qianyi_fz.png')" @click="swapClick"></LImage>
      </div>
      
      <!--（到）服务器-->
      <div class="content bg2">
        <div class="title acenter" @click="privateClick(false)">
          <text class="font28 main-t1">{{T('到')}}</text>
          <div class="width30"></div>
          <text class="font30 text1">{{!toPrivate ? T('公共服务器') : server.name}}</text>
          <text class="font30 text1" v-if="toPrivate">{{'-(' + server.serverCount + ')'}}</text>
          <div class="all"></div>
          <LImage class="arrow-image" v-if="toPrivate" :src="Config.img('xiala.png')"></LImage>
        </div>
      </div>

      <!-- 提示文字-->
      <div class="tipe">
        <text class="font24 text2">{{T('* 迁移服务器将持续数分钟，期间不可操作策略，请耐心等待！')}}</text>
      </div>

      <!-- 确认按钮-->
      <div :class="['ok', selectNum ? 'main1' : 'no1']" @click="okClick">
        <text class="font32 text1">{{T('确认迁移')}}</text>
      </div>

    </div>
  </div>
</template>

<script>


//功能
import Config from "@/_js/config";
import math from "@/_js/math";
import modal from "@/_js/modal";
import broadcast from "@/_js/broadcast";

const weexFunction = weex.requireModule("WeexFunction");
const animation = weex.requireModule("animation");
import weexHttp from "@/_js/https"; ;

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T("迁移策略"));

export default {
  
  data() {
    return {
      T:T,
      Config:Config,
      math:math,
      isShow:false,
      rotate:0, //旋转角度

      totalPairCount:0, // “从”-服务器 交易对总数

      strategys:[], //策略列表
      selectNum:0, //选中策略数量

      toPrivate:true, //true公 => 私
      server: undefined, //选中私有服务器
      servers: undefined, //私有服务器列表
    };
  },

  mounted() {

    setTimeout(() => {
      this.httpMyServers();
    }, 200);
  },

  methods: {

    initServers(datas) {

      var servers = [];
      for (let index = 0; index < datas.length; index++) {
        var item = datas[index];
        if (item.serverStatus == 'ARRANGING') {
          continue;
        }

        //名称
        item.name = '#' + (item.serverMark ? item.serverMark : item.serverCode);
        servers.push(item);
      }
      return servers;
    },

    initStrategys(datas) {

      var totalPairCount = 0;
      for (let index = 0; index < datas.length; index++) {
        var item = datas[index];
        item.select = false;

        var config = item.config;

        var pair = config.tradePairs[0];
        item.apiName = pair.apiName;

        item.childCount = item.executeType == 'FATHER' ? item.childCount : 1;
        totalPairCount = totalPairCount + item.childCount; 
      }

      this.totalPairCount = totalPairCount;

      if(!this.toPrivate) {
        this.server.serverCount =  totalPairCount;
      }
      return datas;
    },

    /********************************** HTTP*/
    //我的服务器列表
    httpMyServers() {
      modal.showLoad();
      var param = {};
      weexHttp.post("server/queryMyServerList",param,true,(data) => {

          if (data.status == 200) {
            data = data.data;
            this.servers = this.initServers(data);
            this.server = this.servers[0];
            this.httpServersStrategy(this.toPrivate, false);
          } else {
            modal.hideLoad();
            modal.toast(data.msg);
          }
        }
      );
    },

    //服务器执行策略列表
    httpServersStrategy(toPrivate, animation) {

      var param = toPrivate ? 
                  {'serverType':'PUBLIC'} :
                  {'id':this.server.id, 'serverType':'PRIVATE'};
      weexHttp.post("server/queryStrategyByServerType",param,true,(data) => {
        
          modal.hideLoad();
          if (data.status == 200) {
            data = data.data;
            this.selectNum = 0;
            this.toPrivate = toPrivate;
            this.strategys = this.initStrategys(data);
            this.isShow = true;
            if (animation) this.rotateAnimation();
          } else {
            modal.toast(data.msg);
          }
        }
      );
    },

    //策略迁移
    httpStrategyMove() {

      modal.showLoad();
      var fromServerType = this.toPrivate ? 'PUBLIC' : 'PRIVATE';
      var toServerType = this.toPrivate ? 'PRIVATE' : 'PUBLIC';

      var executeIds = [];
      for (let index = 0; index < this.strategys.length; index++) {
        var item = this.strategys[index];
        item.select && executeIds.push(item.executeId);
      }
      executeIds = JSON.stringify(executeIds);

      var param = {'fromServerType':fromServerType, 
                    'toServerType':toServerType, 
                    'id':this.server.id,
                    'executeIds':executeIds};
      weexHttp.postX("server/strategyMove",param,true,(data) => {
        
          modal.hideLoad();
          if (data.status == 200) {
            modal.toast(T('迁移成功'));
            broadcast.send(broadcast.EXECUTE_UPDATE);
            this.StrategyMoveOk();
          } else {
            modal.toast(data.msg);
          }
        }
      );
    },
    
    /*************************************************其他*/
    //获取开始时间
    getTime(timestamp){

      var time = new Date(Number(timestamp));

      var year = time.getFullYear();

      var month = time.getMonth() + 1;
      month = month < 10 ? "0" + month : month;

      var day = time.getDate();
      day = day < 10 ? "0" + day : day;

      var hour = time.getHours();
      hour = hour < 10 ? "0" + hour : hour;

      var minute = time.getMinutes();
      minute = minute < 10 ? "0" + minute : minute;

      return year + "/" + month + "/" + day + " " + hour + ":" + minute;
    },

    //旋转
    rotateAnimation() {
      if (!this.$refs.swap) {
        return;
      }

      this.rotate+=180;
      animation.transition(this.$refs.swap, {
        styles: {
          transform: 'rotate('+this.rotate+'deg)',
        },
        duration: 900, //ms
        timingFunction: 'ease',
        needLayout:false,
        delay: 0 //ms
      }, function () {});
    },

    //策略迁移完成
    StrategyMoveOk() {

      var strategys = []
      for (let index = 0; index < this.strategys.length; index++) {
        var item = this.strategys[index];
        !item.select && strategys.push(item);
      }
      this.strategys = strategys;

      this.server.serverCount = this.toPrivate ? 
                                math.jia(this.server.serverCount, this.selectNum) :
                                math.jian(this.server.serverCount, this.selectNum);

      this.selectNum = 0;
    },

    /************************************************* 点击触发*/
    //私有服务器点击
    privateClick(bol) {
      if (this.toPrivate == bol) {
        return;
      }

      var array = [];
      var dict = {};
      for (let index = 0; index < this.servers.length; index++) {
        var server = this.servers[index];
        array.push(server.name);
        dict[server.name] = server;
      }
      
      modal.actionSheet(array, this.server.name, (value)=>{
        this.server = dict[value];
        if(!this.toPrivate) {
          modal.showLoad();
          this.httpServersStrategy(this.toPrivate, false);
        }
      });
    },

    //交换
    swapClick() {

      modal.showLoad();
      this.httpServersStrategy(!this.toPrivate, true);
    },

    //列表点击
    cellClick(item) {

      item.select = !item.select;
      var value = item.select ? 1 : -1;
      this.selectNum = this.selectNum + value;
    },

    //全选
    allClick() {

      var bol = this.selectNum == this.strategys.length;
      this.selectNum = bol ? 0 : this.strategys.length;
      for (let index = 0; index < this.strategys.length; index++) {
        var item = this.strategys[index];
        item.select = !bol;
      }
    },

    //确认迁移
    okClick() {
      if(!this.selectNum) {
        return;
      }

      this.httpStrategyMove();
    },
  },

};

</script>


<style src="@/_css/style.css"></style>
<style scoped>
.bg {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 9;

  padding-left: 20px;
  padding-right: 20px;
  padding-top: 40px;
}
.content {
  border-radius: 24px;
}
.title {
  flex-direction: row;
  height: 110px;

  padding-left: 40px;
  padding-right: 40px;
}
.line {
  height: 1px;
}
.select-image {
  width: 30px;
  height: 30px;
}
.arrow-image{
  width: 20px;
  height: 20px;
  margin-left: 30px;
}


/******************* 提示*/
.tipe {
  margin-top: 40px;
  margin-bottom: 120px;
}

/******************* 列表*/
.list {
  flex: 1;
  margin-top: 20px;
  margin-bottom: 20px;
}
.cell {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  padding-top: 20px;
  padding-bottom: 20px;
  padding-left: 40px;
  padding-right: 35px;
}
.list-tipe {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  flex: 1;
  margin-top: 20px;
  margin-bottom: 20px;
}

/******************* 按钮*/
.swap {
  width: 96px;
  height: 96px;

  margin-top: 14px;
  margin-bottom: 14px;
}
.ok {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  height: 80px;
  border-radius: 8px;
  margin-left: 20px;
  margin-top: 20px;
  margin-bottom: 80px;
}

/******************* 间距*/
.width30 {
  width: 30px;
}
</style>